package com.neusoft.lohas.modules.shop.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.neusoft.lohas.common.config.Global;
import com.neusoft.lohas.common.persistence.Page;
import com.neusoft.lohas.common.persistence.StatusBean;
import com.neusoft.lohas.common.utils.StringUtils;
import com.neusoft.lohas.common.web.BaseShopController;
import com.neusoft.lohas.modules.ivm.entity.order.UserAddress;
import com.neusoft.lohas.modules.ivm.service.order.UserAddressService;
import com.neusoft.lohas.modules.sys.entity.User;
import com.neusoft.lohas.modules.sys.utils.UserUtils;

@Controller
@RequestMapping(value = "${frontPath}/shops/address")
public class UserAddressController extends BaseShopController{

	@Autowired
	private UserAddressService userAddressService;
	
	@ModelAttribute
	public UserAddress get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return userAddressService.get(id);
		}else{
			return new UserAddress();
		}
	}
	
	@RequestMapping(value = {"list", ""})
	public String showAddressList(
			String mobile,
			String username,
			@RequestParam(value="appId", required=false, defaultValue="")String appId,
			HttpServletRequest request, HttpServletResponse response, Model model){
		//处理mobile参数
		if(StringUtils.isNotBlank(mobile))mobile = mobile.split(",")[0];
		User user = (User) UserUtils.getShopUser(mobile, username);
		UserAddress userAddress = new UserAddress();
		userAddress.setUser(user);
		Page<UserAddress> addresses = userAddressService.find(new Page<UserAddress>(request, response), userAddress);
		model.addAttribute("page", addresses);
		model.addAttribute("user", user);
		model.addAttribute("appId", appId);
		return "mobile/shop/address/addressList";
	}
	
	@RequestMapping(value = "form")
	public String form(UserAddress userAddress, 
			@RequestParam(value="appId", required=false, defaultValue="")String appId,
			Model model) {
		User user = UserUtils.getUserById(userAddress.getUser().getId());
		if(!StringUtils.isBlank(userAddress.getId())){
			userAddress = userAddressService.get(userAddress.getId());
		}else{
			userAddress.setName(user.getName());
			userAddress.setPhone(user.getMobile());
		}
		model.addAttribute("address", userAddress);
		model.addAttribute("user", user);
		model.addAttribute("appId", appId);
		return "mobile/shop/address/addressForm";
	}
	
	@RequestMapping(value = "save")
	public String save(UserAddress userAddress, 
			@RequestParam(value="appId", required=false, defaultValue="")String appId,
			Model model, RedirectAttributes redirectAttributes) {
		User user = UserUtils.getUserById(userAddress.getUser().getId());
		//if(userAddress.getDefaultFlag() == 1){
		//	userAddressService.updateDefaultFlag(userAddress.getUser().getId());
		//}
		userAddress.setCreateBy(user);
		userAddress.setUpdateBy(user);
		userAddressService.save(userAddress);
		addMessage(redirectAttributes, "保存收货地址成功");
		return "redirect:"+Global.getFrontPath()+"/shops/address?mobile="+user.getMobile()+"&username="+user.getName()+"&appId="+appId;
	}
	
	@RequestMapping(value = "setDefault")
	public String save(String id, 
			@RequestParam(value="appId", required=false, defaultValue="")String appId,
			RedirectAttributes redirectAttributes) {
		UserAddress userAddress = userAddressService.get(id);
		userAddressService.updateDefaultFlag(userAddress.getUser().getId());
		userAddress.setDefaultFlag(1);
		userAddressService.save(userAddress);
		User user = UserUtils.getUserById(userAddress.getUser().getId());
		addMessage(redirectAttributes, "设置默认地址成功");
		return "redirect:"+Global.getFrontPath()+"/shops/address?mobile="+user.getMobile()+"&username="+user.getName()+"&appId="+appId;
	}
	
	@RequestMapping(value = "delete")
	public String delete(String id, 
			@RequestParam(value="appId", required=false, defaultValue="")String appId,
			RedirectAttributes redirectAttributes) {
		UserAddress userAddress = userAddressService.get(id);
		System.out.println("addressId======"+id);
		User user = UserUtils.getUserById(userAddress.getUser().getId());
		userAddressService.delete(id);
		addMessage(redirectAttributes, "删除收货地址成功");
		return "redirect:"+Global.getFrontPath()+"/shops/address?mobile="+user.getMobile()+"&username="+user.getName()+"&appId="+appId;
	}
	
	@RequestMapping(value = "save/orderAddress")
	public @ResponseBody Object saveOrderAddress(UserAddress userAddress, HttpServletRequest request, HttpServletResponse response) {
		User user = UserUtils.getUserById(userAddress.getUser().getId());
		//if(userAddress.getDefaultFlag() == 1){
		//	userAddressService.updateDefaultFlag(userAddress.getUser().getId());
		//}
		userAddress.setCreateBy(user);
		userAddress.setUpdateBy(user);
		userAddressService.save(userAddress);
		Page<UserAddress> addresses = userAddressService.find(new Page<UserAddress>(request, response), userAddress);
		if (addresses.getList().size()>0) {
			return new StatusBean<List<UserAddress>>(addresses.getList(), StatusBean.SUCCESS, "成功");
		}
		return new StatusBean<List<UserAddress>>(null, StatusBean.ERROR, "失败");
	}
}
